//
//  BabyDao.m
//  parent
//
//  Created by 闫珍 on 15/5/20.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "BabyDao.h"

static NSString *table_name = @"nbg_home_baby_info";

@implementation BabyDao

static BabyDao *baby_dao;

+(BabyDao*)getInstance
{
    if(baby_dao==nil){
        baby_dao = [[BabyDao alloc]init];
    }
    return baby_dao;
}

-(BOOL)saveBabyData:(NSDictionary*)dict
{
    if(dict ==nil){
        debugLog(@"数据为空");
        return NO;
    }else{
        NSUInteger cid = [[dict objectForKey:@"id"]integerValue];
        if(cid>0){
            NSString *cname = [dict objectForKey:@"name"];
            NSUInteger birthy = [[dict objectForKey:@"birthy"]integerValue];
            NSUInteger birthm = [[dict objectForKey:@"birthm"]integerValue];
            NSUInteger birthd = [[dict objectForKey:@"birthd"]integerValue];
            NSUInteger sex = [[dict objectForKey:@"sex"]integerValue];
            NSUInteger classid = [[dict objectForKey:@"classid"]integerValue];
            NSString *classname = @"";
            NSString *schoolname = @"";
            NSUInteger kx_auth = 0;
            NSUInteger schoolid = 0;
            if(classid>0){
                classname = [dict objectForKey:@"classname"];
                schoolid =[[dict objectForKey:@"schoolid"]integerValue];
                schoolname = [dict objectForKey:@"schoolname"];
                kx_auth = [[dict objectForKey:@"kx_auth"]integerValue];
            }
            
            NSUInteger fid = [[dict objectForKey:@"fid"]integerValue];
            NSString *avatar=[dict objectForKey:@"avatar"];
            if([dict objectForKey:@"avatar"] ==[NSNull null] || [dict objectForKey:@"avatar"]==nil){
                avatar= @"";
            }else{
                avatar = [dict objectForKey:@"avatar"];
            }
            
            NSUInteger pid = [[dict objectForKey:@"pid"]integerValue];
            
            
            NSArray *users = [[DBManager getInstanse]selectFrom:table_name items:@[@"cid" ] limits:[NSString stringWithFormat:@" cid =%ld ",cid]];
            NSDictionary *dict = @{@"pid":@(pid),
                                   @"cname":cname,
                                   @"cavatar":avatar,
                                   @"birthy":@(birthy),
                                   @"birthm":@(birthm),
                                   @"birthd":@(birthd),
                                   @"fid":@(fid),
                                   @"sex":@(sex),
                                   @"classid":@(classid),
                                   @"classname":classname,
                                   @"schoolid":@(schoolid),
                                   @"schoolname":schoolname,
                                   @"kx_auth":@(kx_auth),
                                   @"focusid":@(0)
                                   };
            if(users.count>0){
                //更新数据
                
                BOOL result = [[DBManager getInstanse]updateTable:table_name updateDict:dict limits:[NSString stringWithFormat:@" cid =%ld ",cid]];
                if(result) {
                    debugLog(@"更新宝贝信息成功~");
                }else{
                    debugLog(@"更新宝贝信息失败~");
                }
            }else{
                //插入数据
                NSMutableDictionary *insertDic =[[NSMutableDictionary alloc]initWithDictionary:dict];
                [insertDic setObject:@(cid) forKey:@"cid"];
                BOOL result = [[DBManager getInstanse]insertTo:table_name dictionary:insertDic];
                if(result) {
                    debugLog(@"插入宝贝信息成功~");
                }else{
                    debugLog(@"插入宝贝信息失败~");
                }
                
            }
        }
    }
    return YES;
}

-(BOOL)setCurrentBaby:(BabyInfo *)baby
{
    [[DBManager getInstanse]updateTable:table_name updateDict:@{@"focusid":@(0)} limits:@" 1 " ];
    BOOL result = [[DBManager getInstanse]updateTable:table_name updateDict:@{@"focusid":@(1)} limits:[NSString stringWithFormat:@" cid =%ld ",baby.cid]];
    return result;
}

-(BOOL)updateBabyInfo:(NSDictionary *)dict withCid:(NSUInteger)cid
{
    BOOL result = [[DBManager getInstanse]updateTable:table_name updateDict:dict limits:[NSString stringWithFormat:@" cid =%ld ",cid]];
    if(result){
        
    }
    return result;
}

-(BabyInfo*)getBabyInfo:(NSUInteger)cid
{
    BabyInfo *baby = [[BabyInfo alloc]init];
    NSArray *babies = [[DBManager getInstanse]selectFrom:table_name items:@[@"birthd",@"birthm",@"birthy",@"cid",@"cavatar",@"classname",@"classid",@"cname",@"fid",@"kx_auth",@"pid",@"schoolname",@"schoolid",@"sex",@"focusid"] limits:[NSString stringWithFormat:@"cid = %ld",cid]];
    if(babies.count>0){
        NSArray *b = [babies objectAtIndex:0];
        baby.birthd = [[b objectAtIndex:0]integerValue];
        baby.birthm = [[b objectAtIndex:1]integerValue];
        baby.birthy = [[b objectAtIndex:2]integerValue];
        baby.cid = [[b objectAtIndex:3]integerValue];
        baby.cavatar =[b objectAtIndex:4];
        baby.classname = [b objectAtIndex:5];
        baby.classid = [[b objectAtIndex:6]integerValue];
        baby.cname = [b objectAtIndex:7];
        baby.fid = [[b objectAtIndex:8]integerValue];
        baby.kx_auth = [[b objectAtIndex:9]integerValue];
        baby.pid = [[b objectAtIndex:10]integerValue];
        baby.schoolname =[b objectAtIndex:11];
        baby.schoolid = [[b objectAtIndex:12]integerValue];
        baby.sex = [[b objectAtIndex:13]integerValue];
        baby.focusid = [[b objectAtIndex:14]integerValue];
    }
    return baby;
}

-(NSArray*)getBabies:(NSUInteger)fid
{
    NSMutableArray *babs = [[NSMutableArray alloc]init];
    
    NSArray *babies = [[DBManager getInstanse]selectFrom:table_name items:@[@"birthd",@"birthm",@"birthy",@"cid",@"cavatar",@"classname",@"classid",@"cname",@"fid",@"kx_auth",@"pid",@"schoolname",@"schoolid",@"sex",@"focusid"] limits:[NSString stringWithFormat:@"fid = %ld",fid]];
    if(babies.count>0){
        for (NSArray* b in babies) {
            BabyInfo *baby = [[BabyInfo alloc]init];
            baby.birthd = [[b objectAtIndex:0]integerValue];
            baby.birthm = [[b objectAtIndex:1]integerValue];
            baby.birthy = [[b objectAtIndex:2]integerValue];
            baby.cid = [[b objectAtIndex:3]integerValue];
            baby.cavatar =[b objectAtIndex:4];
            baby.classname = [b objectAtIndex:5];
            baby.classid = [[b objectAtIndex:6]integerValue];
            baby.cname = [b objectAtIndex:7];
            baby.fid = [[b objectAtIndex:8]integerValue];
            baby.kx_auth = [[b objectAtIndex:9]integerValue];
            baby.pid = [[b objectAtIndex:10]integerValue];
            baby.schoolname =[b objectAtIndex:11];
            baby.schoolid = [[b objectAtIndex:12]integerValue];
            baby.sex = [[b objectAtIndex:13]integerValue];
            baby.focusid = [[b objectAtIndex:14]integerValue];
            [babs addObject:baby];
        }
       
    }
    return babs;
}

-(BOOL)deleteBabyById:(NSUInteger)bid
{
    BOOL result = [[DBManager getInstanse]deleteFrom:table_name limits:[NSString stringWithFormat:@" id = %ld",bid]];
    return result;
}
@end
